#!/bin/bash


echo '开始部署'
if [ ! -d "/opt/dengtacj" ];then
   mkdir -p "/opt/dengtacj"
fi
cd /opt/dengtacj

if [ -d "/opt/dengtacj/news-web" ]
then
    echo '文件夹存在'
    # shellcheck disable=SC2164
    cd /opt/dengtacj/news-web
    echo '1.正在停止容器'
    docker-compose down
    echo '2.删除代码目录'
    # rm -rf /opt/dengtacj/news-web
    echo '删除代码目录 正在停止容器 success'
    sleep 1
else
    echo '文件夹不存在'
    # shellcheck disable=SC2046
    docker stop $(docker ps -a  |grep news-web | awk -F " " '{print $1}')
    # shellcheck disable=SC2046
    docker rm $(docker ps -a  |grep news-web | awk -F " " '{print $1}')
    # rm -rf /opt/dengtacj/news-web
    echo '手动停止删除容器成功'
fi


echo '删除已废弃的 images'
docker stop $(docker ps -a  |grep news-web | awk -F " " '{print $1}')
docker rm $(docker ps -a  |grep news-web | awk -F " " '{print $1}')
docker rmi $(docker images  |grep none | awk -F " " '{print $2}')
docker rmi $(docker images  |grep none | awk -F " " '{print $3}')
